<template>
  <div class="warehouse">
    <!-- 列表页面 -->
    <!-- <div class="container" v-if="!showEdit">
      <div class="header">
        <div class="title">仓库列表</div>
        <div>
          <el-button @click="exprotExcel">导出</el-button>
        </div>
      </div> -->
      <!-- 表格 -->
      <!-- <lin-table
        :tableColumn="tableColumn"
        :tableData="tableData"
        :operate="operate"
        @handleEdit="handleEdit"
        @row-click="rowClick"
        v-loading="loading"
      ></lin-table>
    </div> -->

    <!-- 编辑页面 -->
    
    <div class="warehouse" v-if="showEdit == 0">
      <el-card class="box-card" shadow="hover" v-for="(item,index) in tableData" :key="index" >
        <div slot="header" class="clearfix">
          <!-- <span></span> -->
          <i class="el-icon-s-home" style="font-size: 20px;"></i>
          <div style="float: right; padding: 3px 0">{{item.country}}</div>
        </div>
        <div class="content" @click.stop="addArea(item)">
          <div class="edit">
            <i style="font-size: 20px;cursor: pointer" class="el-icon-edit" @click.stop="addedit(item)"></i>
          </div>
          <div class="code">{{item.code}}</div>
          <div class="item">{{item.name}}</div>
          <div class="item">{{item.company}}</div>
          <div class="item">{{item.size}}</div>
        </div>
        
      </el-card>
    </div>
    <wh-edit v-else-if="showEdit == 1" @editClose="editClose" :editWarehouseID="editWarehouseID"></wh-edit>
    <wh-area v-else @editClose="editClose" :warehouseCode="warehouseCode"></wh-area>
  </div>
</template>

<script>
import warehouse from '@/model/warehouse'
import LinTable from '@/component/base/table/lin-table'
// import BookModify from './book-modify'
import ParseTime from '@/lin/util/parse-time'
import WhEdit from './wh-edit.vue'
import WhArea from './wh-area.vue'

export default {
  components: {
    LinTable,
    WhEdit,
    WhArea,
    // BookModify,
  },
  data() {
    return {
      refreshPagination: true, // 页数增加的时候，因为缓存的缘故，需要刷新Pagination组件
      currentPage: 1, // 默认获取第一页的数据
      pageCount: 10, // 每页10条数据
      total_nums: 0, // 分组内的用户总数
      tableColumn: [
        { prop: 'name', label: '仓库名称' },
        { prop: 'size', label: '面积' },
        { prop: 'phone', label: '手机号' },
        { prop: 'company', label: '公司名字' },
        { prop: 'contactor', label: '联系人' },
        { prop: 'type', label: '类型' },
        { prop: 'status', label: '状态' },
        { prop: 'email', label: '邮箱' },
        { prop: 'postcode', label: '邮编' },
        { prop: 'regionals', label: '地域' },
        { prop: 'country', label: '国家' },
        { prop: 'province', label: '州/省' },
        { prop: 'city', label: '市/区县' },
        { prop: 'street', label: '街道' },
        { prop: 'apartment', label: '单位/公寓/套房编号' },
        { prop: 'description', label: '描述' },
      ],
      tableData: [],
      operate: [],
      showEdit: 0,
      editWarehouseID: 1,
      showArea: false,
      warehouseCode: ''
    }
  },
  async created() {
    this.loading = true
    await this.getWarehouse()
    this.operate = [{ name: '编辑', func: 'handleEdit', type: 'primary' }]
    this.loading = false
  },
  methods: {
    async getWarehouse() {
      console.log(222)
      try {
        const warehouseList = await warehouse.getWarehouseList()
        this.tableData = warehouseList
        console.log(this.tableData)
      } catch (error) {
        if (error.code === 10020) {
          this.tableData = []
        }
      }
    },
    async handleCurrentChange(val) {
      this.currentPage = val
      this.loading = true
      await this.getAdminUsers('changePage')
      this.loading = false
    },
    handleEdit(val) {
      console.log('val', val)
      this.showEdit = 1
      this.editWarehouseID = val.row.code
    },
    addedit(item) {
      console.log(2222);
      this.showEdit = 1
      this.editWarehouseID = item.code
    },
    rowClick() {},
    editClose() {
      this.showEdit = 0
      this.getWarehouse()
    },
    async addArea(item) {
      this.showEdit = 3
      this.warehouseCode = item.code
     
    },
    // 导出表格
    exprotExcel() {
      // 动态导入
      import('@/lin/util/export-excel').then(excel => {
        const tHeader = ['timestamp', 'title', 'label', 'importance', 'status']
        const filterVal = ['timestamp', 'title', 'label', 'importance', 'status']
        const data = this.formatJson(filterVal)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'table-list',
        })
      })
    },
    // 将表单格式化为json数据
    formatJson(filterVal) {
      return this.tableData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return ParseTime(v[j])
        }
        return v[j]
      }),)
    },
  },
}
</script>

<style lang="scss" scoped>
.container {
  padding: 0 30px;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .title {
      height: 59px;
      line-height: 59px;
      color: $parent-title-color;
      font-size: 16px;
      font-weight: 500;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
    margin: 20px;
  }
}
.warehouse {
  padding: 20px;
}
.warehouse {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  .box-card {
    width: 500px;
    margin-right: 20px;
    margin-bottom: 30px;
    .content {
      position: relative;
      cursor: pointer;
      .edit {
        width: 30px;
        height: 30px;
        position: absolute;
        top: 10px;
        right: 10px;
      }
    }
    .code {
      font-size: 22px;
      font-weight:bolder;
      margin-bottom: 10px;
    }
    .item {
      margin-bottom: 15px;
    }
  }
}
</style>
